﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Linq;
using System.Data;
using System.Data.SqlClient;
using Gem_Tools_Entity.Gem_Tools;
using Gem_Tools_DataLayer;
using Gem_Tools_Utility;

namespace Gem_Tools_Controller.Gem_Tools
{
    public class CAssign_Task_Controller
    {
        private void MapAssign_Task(DataRow p_row, CAssign_Task p_objDes)
        {
            DataTable p_dt = p_row.Table;

            if (p_dt.Columns.Contains("Auto_ID"))
                p_objDes.Auto_ID = CUtility.Convert_To_Int32(p_row["Auto_ID"]);

            if (p_dt.Columns.Contains("Stage_Or_Part_ID"))
                p_objDes.Stage_Or_Part_ID = CUtility.Convert_To_Int32(p_row["Stage_Or_Part_ID"]);

            if (p_dt.Columns.Contains("Staff_ID"))
                p_objDes.Staff_ID = CUtility.Convert_To_Int32(p_row["Staff_ID"]);

            if (p_dt.Columns.Contains("Start_Date"))
                p_objDes.Start_Date = CUtility.Convert_To_DateTime(p_row["Start_Date"]);

            if (p_dt.Columns.Contains("End_Date"))
                p_objDes.End_Date = CUtility.Convert_To_DateTime(p_row["End_Date"]);

            if (p_dt.Columns.Contains("Created"))
                p_objDes.Created = CUtility.Convert_To_DateTime(p_row["Created"]);

            if (p_dt.Columns.Contains("Created_By"))
                p_objDes.Created_By = CUtility.Convert_To_String(p_row["Created_By"]);

            if (p_dt.Columns.Contains("Deleted"))
                p_objDes.Deleted = CUtility.Convert_To_Int32(p_row["Deleted"]);

            if (p_dt.Columns.Contains("Last_Updated"))
                p_objDes.Last_Updated = CUtility.Convert_To_DateTime(p_row["Last_Updated"]);

            if (p_dt.Columns.Contains("Last_Updated_By"))
                p_objDes.Last_Updated_By = CUtility.Convert_To_String(p_row["Last_Updated_By"]);

        }

        public IList<CAssign_Task> List_Assign_Task()
        {
            IList<CAssign_Task> v_arrAssign_Task = new List<CAssign_Task>();
            DataTable v_dt = new DataTable();

            try
            {
                CSqlHelper.FillDataTable(CConfig.g_strGem_Tools_Conn_String, v_dt, "sp_sel_List_Assign_Task");

                foreach (DataRow v_row in v_dt.Rows)
                {
                    CAssign_Task v_objAssign_Task = new CAssign_Task();
                    MapAssign_Task(v_row, v_objAssign_Task);
                    v_arrAssign_Task.Add(v_objAssign_Task);
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                v_dt.Dispose();
            }

            return v_arrAssign_Task;
        }

        public CAssign_Task Get_Assign_Task_By_ID(int p_iID)
        {
            CAssign_Task v_objAssign_Task = null;
            DataTable v_dt = new DataTable();

            try
            {
                CSqlHelper.FillDataTable(CConfig.g_strGem_Tools_Conn_String, v_dt, "sp_sel_Get_Assign_Task_By_ID", p_iID);

                if (v_dt.Rows.Count > 0)
                {
                    v_objAssign_Task = new CAssign_Task();
                    MapAssign_Task(v_dt.Rows[0], v_objAssign_Task);
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                v_dt.Dispose();
            }

            return v_objAssign_Task;
        }

        public int Insert_Assign_Task(CAssign_Task p_objData)
        {
            int v_iRes = CConst.INT_VALUE_NULL;

            try
            {
                v_iRes = Convert.ToInt32(CSqlHelper.ExecuteScalar(CConfig.g_strGem_Tools_Conn_String, "sp_ins_Assign_Task",
                            p_objData.Stage_Or_Part_ID, p_objData.Staff_ID, p_objData.Start_Date, p_objData.End_Date, p_objData.Last_Updated_By).ToString());

            }
            catch (Exception ex)
            {
                throw ex;
            }


            return v_iRes;
        }

        public void Update_Assign_Task(CAssign_Task p_objData)
        {
            try
            {
                CSqlHelper.ExecuteNonquery(CConfig.g_strGem_Tools_Conn_String, "sp_upd_Assign_Task",
                            p_objData.Auto_ID, p_objData.Stage_Or_Part_ID, p_objData.Staff_ID, p_objData.Start_Date, p_objData.End_Date, p_objData.Last_Updated_By);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public void Delete_Assign_Task(int p_iID, string p_strLast_Updated_User)
        {
            try
            {
                CSqlHelper.ExecuteNonquery(CConfig.g_strGem_Tools_Conn_String, "sp_del_Assign_Task", p_iID, p_strLast_Updated_User);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

    }
}
